/**
 * 여행지 정보 수정 script
 */
var xhr; 
	function createXMLHttpRequest(){
		if(window.ActiveXObject){// ms explorer일 경우 
			xhr=new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			xhr=new XMLHttpRequest();
		}
	}
	//0. 지역정보의 수정값을 서버로 보내준다.
	function startUpdateTravelZone(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackUpdateTravelZone; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");
		var locationid=document.getElementById("locationid").value;
		var location=document.getElementById("location").value;
		var zoneinfo=document.getElementById("zoneinfo").value;
		var zoneimg=document.getElementById("zoneimg").value;
		if(zoneinfo==""){
			zoneinfo=document.getElementById("zoneinfoValue").innerHTML;
		}
		if(zoneimg==""){
			zoneimg=document.getElementById("zoneimgValue").innerHTML;
		}
		xhr.send("mode=updateTravelZone&locationid="
				+locationid+"&location="+location+"&zoneinfo="
				+zoneinfo+"&zoneimg="+zoneimg);
	}
	function callbackUpdateTravelZone(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				clearUpdateTravelZone();
			}
		}
	}
	//지역 정보 수정 후 창 지워주기
	function clearUpdateTravelZone(){
		clearRadioButton(document.getElementsByName("locationid"));
		document.getElementById("locationid").value="";
		document.getElementById("location").value="";
		document.getElementById("zoneinfo").value="";
		document.getElementById("zoneimg").value="";
		document.getElementById("zoneinfoValue").innerHTML="";
		document.getElementById("zoneimgValue").innerHTML="";
	}
	/////////////////////////////////////////////////////////
	
	//1. 여행지 정보의 수정값을 서버로 보내준다.
	function startUpdateTravelInfo(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackUpdateTravelInfo; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");
		var place=checkedRadioButton(document.getElementsByName("place"));
		var pnumber=document.getElementById("pnumber").value;
		var address=document.getElementById("address").value;
		var phomepage=document.getElementById("phomepage").value;
		var information=document.getElementById("information").value;
		var traffic=document.getElementById("traffic").value;
		var map=document.getElementById("map").value;
		if(traffic==""){
			traffic=document.getElementById("trafficValue").innerHTML;
			alert("tra:"+traffic);
		}
		if(map==""){
			map=document.getElementById("mapValue").innerHTML;
		}
		xhr.send("mode=updateTravelInfo&place="+place+"&pnumber="+pnumber
				+"&address="+address+"&phomepage="+phomepage
				+"&information="+information+"&traffic="+traffic+"&map="+map);
	}
	function callbackUpdateTravelInfo(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				clearUpdateTravelInfo();
			}
		}
	}
	//여행지 정보 수정 후 창 지워주기
	function clearUpdateTravelInfo(){
		clearRadioButton(document.getElementsByName("locationid"));
		document.getElementById("placelist").innerHTML="";
		document.getElementById("pnumber").value="";
		document.getElementById("address").value="";
		document.getElementById("phomepage").value="http://";
		document.getElementById("information").value="";
		document.getElementById("traffic").value="";
		document.getElementById("trafficValue").innerHTML="";
		document.getElementById("map").value="";
		document.getElementById("mapValue").innerHTML="";
	}
	/////////////////////////////////////////////////////////
	
	// 2.여행지 이미지 수정
	function startUpdatePlaceImage(data,i){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackUpdatePlaceImage; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var place=checkedRadioButton(document.getElementsByName("place"));
		var gallery=document.getElementById("gallery"+i).value;
		xhr.send("mode=updatePlaceImage&place="+place+"&gallery="+gallery+"&data="+data);
	}
	function callbackUpdatePlaceImage(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				clearUpdatePlaceImage();
			}
		}
	}
	// 이미지 값 수정 후 창 지워주기
	function clearUpdatePlaceImage(){
		clearRadioButton(document.getElementsByName("locationid"));
		document.getElementById("placelist").innerHTML="";
		document.getElementById("galleryValue").innerHTML="";
	}
	/////////////////////////////////////////////////
	
	// 3. 숙박 정보 수정
	function startUpdatePlaceVacancy(i){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackUpdatePlaceVacancy; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");
		var stay=document.getElementById("stay"+i).value;
		var place=checkedRadioButton(document.getElementsByName("place"));
		var vnumber=document.getElementById("vnumber"+i).value;
		var vhomepage=document.getElementById("vhomepage"+i).value;
		xhr.send("mode=updatePlaceVacancy&stay="+stay+"&place="+place+"&vnumber="+vnumber
				+"&vhomepage="+vhomepage);
	}
	function callbackUpdatePlaceVacancy(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				clearUpdatePlaceVacancy();
			}
		}
	}
	// 숙박정보 수정 후 창 지워주기
	function clearUpdatePlaceVacancy(){
		clearRadioButton(document.getElementsByName("locationid"));
		document.getElementById("placelist").innerHTML="";
		document.getElementById("vacancyValue").innerHTML="";
	}
	/////////////////////////////////////////////////////////
	
	// 4. 맛집 정보 수정
	function startUpdatePlaceFr(i){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackUpdatePlaceFr; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");
		var restaurant=document.getElementById("restaurant"+i).value;
		var place=checkedRadioButton(document.getElementsByName("place"));
		var fnumber=document.getElementById("fnumber"+i).value;
		var fhomepage=document.getElementById("fhomepage"+i).value;
		xhr.send("mode=updatePlaceFr&restaurant="+restaurant+"&place="+place+"&fnumber="+fnumber
				+"&fhomepage="+fhomepage);
	}
	function callbackUpdatePlaceFr(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				clearUpdatePlaceFr();
			}
		}
	}
	// 맛집정보 수정 후 창 지워주기
	function clearUpdatePlaceFr(){
		clearRadioButton(document.getElementsByName("locationid"));
		document.getElementById("placelist").innerHTML="";
		document.getElementById("frValue").innerHTML="";
	}
	//////////////////////////////////////////////////////
	
	// 5. 공지사항 수정
	function startUpdatePlaceNotice(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackUpdatePlaceNotice; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var place=checkedRadioButton(document.getElementsByName("place"));
		var notice=document.getElementById("notice").value;
		xhr.send("mode=updatePlaceNotice&place="+place+"&notice="+notice);
	}
	function callbackUpdatePlaceNotice(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				clearUpdatePlaceNotice();
			}
		}
	}
	// 공지사항 수정 후 창 지워주기
	function clearUpdatePlaceNotice(){
		clearRadioButton(document.getElementsByName("locationid"));
		document.getElementById("placelist").innerHTML="";
		document.getElementById("notice").value="";
	}
	///////////////////////////////////////////////////
	
	// 6. 댓글 수정
	function startUpdatePlaceComment(id){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackUpdatePlaceComment; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var place=document.getElementById("place").value;
		var memberid=id;
		var content=document.getElementById("content").value;
		xhr.send("mode=updatePlaceComment&place="+place+"&memberid="+memberid
				+"&content="+content);
	}
	function callbackUpdatePlaceComment(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				clearUpdatePlaceComment();
				var place=document.getElementById("place").value;
				var locationId=document.getElementById("locationId").value;
				location.href="/wetogether/travelinfo_output.do?mode=outputTravelInfo&place="
					+place+"&locationId="+locationId;
			}
		}
	}
	// 댓글 수정 후 창 지워주기
	function clearUpdatePlaceComment(){
		document.getElementById("commentValue").innerHTML="";
	}
	//////////////////////////////////////////////////////
	
	// 댓글 내용 가져오기
	function startGetPlaceComment(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackGetPlaceComment; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var place=document.getElementById("place").value;
		var memberid="java";
		xhr.send("mode=getPlaceComment&place="+place+"&memberid="+memberid);
	}
	function callbackGetPlaceComment(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				var jsonData=eval('('+xhr.responseText+')');
				if(jsonData.flag=="commentValue"){
					document.getElementById("commentValue").innerHTML=createCommentFile(jsonData);
				}else{
					document.getElementById("commentValue").innerHTML="";
				}
			}
		}
	}
	// 댓글 가져온 정보 새로 만들어서 보여주기
	function createCommentFile(data){
		var dataValue="<br>";
		if(data.recommend=="1"){
			dataValue+="ID : "+data.memberid+" | 추천^^<br>";
		}else{
			dataValue+="ID : "+data.memberid+" | 추천 안 함-_-<br>";
		}
		dataValue+="<textarea id=content cols=20 rows=5>"+data.content+"</textarea><br>";
		dataValue+="<input type=button value=수정 onclick=startUpdatePlaceComment('"+data.memberid+"')></input>";
		dataValue+="<input type=button value=삭제 onclick=startDeletePlaceComment('"+data.memberid+"','"+data.recommend+"')></input>";
		return dataValue;
	}
	///////////////////////////////////////////////////
	
	// 해당 지역의 여행지 리스트 값 가져오기
	function selectZone(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackSeleteZone; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var locationid=checkedRadioButton(document.getElementsByName("locationid"));
		var change=document.getElementById("change").value;
		xhr.send("mode=findPlaceList&locationid="+locationid+"&change="+change);
	}
	function callbackSeleteZone(){
		if(xhr.readyState==4){		
			if(xhr.status==200){
				var jsonData=eval('('+xhr.responseText+')');
				if(jsonData.Emessage==null){
					if(jsonData.change=="zone"){ //지역정보
						document.getElementById("locationid").value=jsonData.locationid;
						document.getElementById("location").value=jsonData.location;
						document.getElementById("zoneinfoValue").innerHTML=jsonData.zoneinfo;
						document.getElementById("zoneimgValue").innerHTML=jsonData.zoneimg;
					}else{
						document.getElementById("placelist").innerHTML=createRadioButton(jsonData.info);
					}
				}else{
					if(jsonData.change=="zone"){
						document.getElementById("error").innerHTML=jsonData.Emessage;
					}else{
						document.getElementById("placelist").innerHTML=jsonData.Emessage;
					}
				}
				// 다시 지역을 선택했을 때 정보값을 지워준다.
				if(jsonData.change=="info"){ // 여행지 정보
					document.getElementById("pnumber").value="";
					document.getElementById("address").value="";
					document.getElementById("phomepage").value="http://";
					document.getElementById("information").value="";
					document.getElementById("trafficValue").innerHTML="";
					document.getElementById("mapValue").innerHTML="";
				}else if(jsonData.change=="image"){ // 이미지 정보
					document.getElementById("galleryValue").innerHTML="";
				}else if(jsonData.change=="vacancy"){ // 숙박 정보
					document.getElementById("vacancyValue").innerHTML="";
				}else if(jsonData.change=="fr"){ // 맛집 정보
					document.getElementById("frValue").innerHTML="";
				}else if(jsonData.change=="notice"){ // 공지사항
					document.getElementById("notice").value="";
				}else if(jsonData.change=="comment"){ // 댓글
					document.getElementById("memberValue").innerHTML="";
				}
				document.getElementById("error").innerHTML="";
			}
		}
	}
	/////////////////////////////////////////
	
	// 선택한 여행지 정보를 가져온다
	function selectPlace(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackSeletePlace; 
		xhr.open("POST","/wetogether/travelinfo_update.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var place=checkedRadioButton(document.getElementsByName("place"));
		var change=document.getElementById("change").value;
		xhr.send("mode=getTravelInfo&place="+place+"&change="+change);
	}
	function callbackSeletePlace(){
		if(xhr.readyState==4){		
			if(xhr.status==200){
				var jsonData=eval('('+xhr.responseText+')');
				document.getElementById("error").innerHTML="";
				if(jsonData.Emessage==null){
					if(jsonData.flag=="info"){ // 여행지 정보
						document.getElementById("pnumber").value=jsonData.pnumber;
						document.getElementById("address").value=jsonData.address;
						document.getElementById("phomepage").value=jsonData.phomepage;
						document.getElementById("information").value=jsonData.information;
						document.getElementById("trafficValue").innerHTML=" "+jsonData.traffic;
						document.getElementById("mapValue").innerHTML=" "+jsonData.map;
					}else if(jsonData.flag=="image"){ // 이미지 리스트
						document.getElementById("galleryValue").innerHTML=createImgFile(jsonData.img);
					}else if(jsonData.flag=="vacancy"){ // 숙박 리스트
						document.getElementById("vacancyValue").innerHTML=createVacancyFile(jsonData.vacancy);
					}else if(jsonData.flag=="fr"){ // 맛집 리스트
						document.getElementById("frValue").innerHTML=createFrFile(jsonData.fr);
					}else if(jsonData.flag=="notice"){ // 공지사항
						document.getElementById("notice").value=jsonData.notice;
					}else if(jsonData.flag=="comment"){ // 댓글 리스트
						document.getElementById("memberValue").innerHTML
						=createCommentListFile(jsonData.comment);
					}
				}else{
					if(jsonData.flag=="img"){
						document.getElementById("galleryValue").innerHTML=jsonData.Emessage;
					}else if(jsonData.flag=="vacancy"){
						document.getElementById("vacancyValue").innerHTML=jsonData.Emessage;
					}else if(jsonData.flag=="fr"){
						document.getElementById("frValue").innerHTML=jsonData.Emessage;
					}else if(jsonData.flag=="notice"){
						document.getElementById("notice").value="";
						document.getElementById("error").innerHTML=jsonData.Emessage;
					}else if(jsonData.flag=="comment"){
						document.getElementById("memberValue").innerHTML=jsonData.Emessage;
					}else{
						document.getElementById("error").innerHTML=jsonData.Emessage;
					}
				}
			}
		}
	}
	// 1. 이미지의 리스트 사이즈 만큼 수정 가능하게 하기 위해 생성
	function createImgFile(data){
		var dataValue="<br>";
		for(i=0; i<data.length; i++){
			dataValue+="<input type=file id=gallery"+(i+1)+" size=30></input> "+data[i]+"<br>";
			dataValue+="<input type=button value=수정 onclick=startUpdatePlaceImage('"+data[i]+"',"+(i+1)+")></input>";
			dataValue+="<input type=button value=삭제 onclick=startDeletePlaceImage('"+data[i]+"')></input><br><br>";
			}
		return dataValue;
	}
	// 2. 숙박정보의 리스트 사이즈 만큼 수정 가능하도록 하기 위해 생성
	function createVacancyFile(data){
		var dataValue="<br>";
		for(i=0; i<data.length; i++){
			dataValue+="숙 박 명  "+"<input type=text id=stay"+(i+1)+" value="+data[i].stay+" size=30 readonly></input><br>";
			dataValue+="전화번호 <input type=text id=vnumber"+(i+1)+" value="+data[i].vnumber+" size=30></input><br>";
			dataValue+="홈페이지 <input type=text id=vhomepage"+(i+1)+" value="+data[i].vhomepage+" size=30></input><br>";
			dataValue+="<input type=button value=수정 onclick=startUpdatePlaceVacancy("+(i+1)+")></input>";
			dataValue+="<input type=button value=삭제 onclick=startDeletePlaceVacancy('"+data[i].stay+"')></input><br><br>";
		}
		return dataValue;
	}
	// 3. 맛집정보의 리스트 사이즈 만큼 수정 가능하도록 하기 위해 생성
	function createFrFile(data){
		var dataValue="<br>";
		for(i=0; i<data.length; i++){
			dataValue+="맛 집 명  "+"<input type=text id=restaurant"+(i+1)+" value="+data[i].restaurant+" size=30 readonly></input><br>";
			dataValue+="전화번호 <input type=text id=fnumber"+(i+1)+" value="+data[i].fnumber+" size=30></input><br>";
			dataValue+="홈페이지 <input type=text id=fhomepage"+(i+1)+" value="+data[i].fhomepage+" size=30></input><br>";
			dataValue+="<input type=button value=수정 onclick=startUpdatePlaceFr("+(i+1)+")></input>";
			dataValue+="<input type=button value=삭제 onclick=startDeletePlaceFr('"+data[i].restaurant+"')></input><br><br>";
		}
		return dataValue;
	}
	//4. 댓글 리스트 사이즈 만큼 삭제 가능하도록 하기 위해 생성
	function createCommentListFile(data){
		var dataValue="<br>";
		for(i=0; i<data.length; i++){
			dataValue+="회원ID : "+data[i].memberid+" / "+data[i].place+" : "+data[i].recommend+"<br>";
			dataValue+="<textarea id=content cols=30 rows=5>"+data[i].content+"</textarea><br>";
			dataValue+="<input type=button value=삭제 onclick=startDeletePlaceCommentValue('"+data[i].memberid+"','"+data[i].recommend+"')></input><br><br>";
		}
		return dataValue;
	}
	
	////////////////////////////////////////
	
	// 선택된 라디오버튼 값을 리턴
	function checkedRadioButton(data){
		var dataValue;
		for(i=0; i<data.length; i++){
			if(data[i].checked){
				dataValue=data[i].value;
				break;
			}
		}
		return dataValue;
	}
	// 라디오버튼 선택 해제
	function clearRadioButton(data){
		for(i=0; i<data.length; i++){
			data[i].checked=false;
		}
	}
	// 리스트의 값 만큼 라디오버튼 생성
	function createRadioButton(obj){
		var str="";
		for(i=0; i<obj.length; i++){
			str+="<input type='radio' name='place' value="+obj[i]+" onchange='selectPlace()'></input>"+obj[i];
		}
		return str;
	}
	///////////////////////////////////////////
	